package com.atguigu.linkedlist;

import java.util.Stack;

/**
 * @author durant
 * @create 2021-11-11 19:58
 */
public class SingleLinkedListDemo {
    public static void main(String[] args) {
        //测试
        HeroNode hero1 = new HeroNode(1, "松江1", "及时雨1");
        HeroNode hero2 = new HeroNode(2, "松江2", "及时雨2");
        HeroNode hero3 = new HeroNode(3, "松江3", "及时雨3");
        HeroNode hero4 = new HeroNode(4, "松江4", "及时雨4");
        HeroNode hero5 = new HeroNode(5, "松江4", "及时雨4");
        HeroNode hero6 = new HeroNode(6, "松江4", "及时雨4");
        HeroNode hero7 = new HeroNode(7, "松江4", "及时雨4");
        HeroNode hero8 = new HeroNode(8, "松江4", "及时雨4");
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addById(hero3);
        singleLinkedList.addById(hero7);
        singleLinkedList.addById(hero1);
        singleLinkedList.addById(hero2);
        singleLinkedList.addById(hero8);
        singleLinkedList.addById(hero4);
        singleLinkedList.addById(hero5);
        singleLinkedList.addById(hero6);
        singleLinkedList.show();
        System.out.println("单链表长度：" + SingleLinkedList.getLength(singleLinkedList.getHead()));
        System.out.println("：" + SingleLinkedList.getHeroByDesc(singleLinkedList.getHead(), 9));
        System.out.println("单链表反转");
        SingleLinkedList.reverse(singleLinkedList.getHead());
        singleLinkedList.show();
        System.out.println("反转打印");
        SingleLinkedList.reversePrint(singleLinkedList);
    }

}


// 管理英雄
class SingleLinkedList {
    // 创建一个头节点
    private HeroNode head = new HeroNode(-1, "", "");

    //提供head
    public HeroNode getHead() {
        return head;
    }
    //逆序打印单链表
    // 方式一 先将单链表反转再去打印，这样做会破坏原来的单链表结构，不可取
    // 可以利用栈这个数据结构，将各个节点压入栈中，利用先进后出的原则进行打印
    public static void reversePrint(SingleLinkedList singleLinkedList){
        //使用方式二来实现
        Stack<String> stack=new Stack<>();
        HeroNode head = singleLinkedList.getHead();
            HeroNode temp =head.next;
       while (temp != null){
            stack.add(temp.toString());
            temp = temp.next;
       }

        while (stack.size()>0){
            System.out.println(stack.pop());
        }
    }
    // 单链表反转
    // 没遍历一个链表，就放到新链表的前端
    public static void reverse(HeroNode head) {
       if(head.next == null ||head.next.next == null){
           return;
       }
        HeroNode temp=head.next;
        HeroNode tempNext =null;
        HeroNode reverseHead =new HeroNode(0, "", "");
        while (temp != null){
            tempNext = temp.next;
            temp.next=reverseHead.next;
            reverseHead.next=temp;
            temp = tempNext;
        }
        head.next = reverseHead.next;
    }

    // 获取倒数第k个节点
    public static HeroNode getHeroByDesc(HeroNode head, int k) {
        //判断链表为空
        if (head.next == null) {
            return null;
        }
        // 遍历链表
        int size = getLength(head);
        if (k < 0 || k > size) {
            System.out.println("");
            return null;
        }
        HeroNode temp = head.next;
        for (int i = 0; i < (size - k); i++) {
            temp = temp.next;
        }
        return temp;


    }

    // 删除节点
    public void delete(int no) {
        // 判断是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head;

        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;//表示遍历结束
            }

            if (no == temp.next.no) {
                flag = true;
                break;
            }

            temp = temp.next;
        }
        if (flag == true) {
            temp.next = temp.next.next;
        } else {
            System.out.println("没有找到标号：" + no + "的英雄");
        }


    }

    // 通过编号进行添加
    public void addById(HeroNode heroNode) {
        if (head.next == null) {
            head.next = heroNode;
            return;
        }
        HeroNode temp = head.next;
        if (heroNode.getNo() < temp.getNo()) {
            head.next = heroNode;
            heroNode.next = temp;
            return;
        }
        while (true) {
            // 当节点相同时直接报错
            if (heroNode.getNo() == temp.getNo()) {
                System.out.println(heroNode.getNo() + "该节点已存在");
                break;
            }
            //当节点的下一个为空时，直接进行添加
            if (temp.next == null) {
                temp.next = heroNode;
                break;
            }
            // 当加入的节点的no 比要加入点要小时，则中断temp下移
            // 将该节点添加在这个temp后面
            if (heroNode.getNo() > temp.getNo() && heroNode.getNo() < temp.next.getNo()) {
                HeroNode tempNext = temp.next;
                temp.next = heroNode;
                heroNode.next = tempNext;
                break;
            }

            temp = temp.next;

        }
    }

    // 添加节点到当前方法
    //思路，当不考虑编号顺序时，
    // 1。找到当前链表的最后节点
    // 2.将最后这个节点的next 指向新的节点
    public void add(HeroNode heroNode) {
        //因为head节点不动，因此需要一个辅助节点遍历temp
        HeroNode temp = head;
        while (true) {
            //找到链表的最后
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    //修改信息  id不能改
    public void update(HeroNode heroNode) {
        // 判断是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;//表示遍历结束
            }
            if (heroNode.no == temp.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag == true) {
            temp.name = heroNode.name;
            temp.nickname = heroNode.nickname;
        } else {
            System.out.println("没有找到标号：" + heroNode.no + "的英雄");
        }


    }

    //显示链表
    public void show() {


        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        while (true) {
            System.out.println(temp);
            if (temp.next == null) {
                break;
            }
            temp = temp.next;

        }

    }

    //获取单链表的有效数据的个数
    public static int getLength(HeroNode head) {
        if (head.next == null) {
            return 0;
        }
        int length = 0;
        HeroNode current = head.next;
        while (current != null) {
            length++;
            current = current.next;
        }
        return length;
    }
}

class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;// 指向下一个节点

    public int getNo() {
        return no;
    }

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' + '}';
    }
}